package com.duowei.diandan.adapter;

import android.annotation.SuppressLint;
import android.view.View;

import com.duowei.diandan.bean.food.FoodImpl;
import com.duowei.diandan.bean.food.PresentOrAddPrice;
import com.duowei.diandan.databinding.CommonFoodItemBinding;

import java.util.List;

/**
 * 赠送/加价Adapter
 */

public class PresentOrAddPriceAdapter extends CommonFoodAdapter {

    private List<PresentOrAddPrice> mList;

    public PresentOrAddPriceAdapter(List<PresentOrAddPrice> foodList) {
        super(foodList);
        mList = foodList;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void setupView(CommonFoodItemBinding binding, FoodImpl food) {
        super.setupView(binding, food);
        PresentOrAddPrice item = (PresentOrAddPrice) food;

        //
        if (item.isShowPrice()) {
            binding.include.priceDescView.setVisibility(View.VISIBLE);
            binding.include.priceDescView.setPrice(food.getPrice());

            String price = String.valueOf(food.getPrice()).replaceAll("\\.?0*$", "");
            binding.include.priceDescView.setText("+¥" + price);

        } else {
            binding.include.priceDescView.setVisibility(View.GONE);
            binding.include.priceDescView.setPrice(null);
        }

        //
        if (item.isChecked()) {
            binding.checkedLayout.setVisibility(View.VISIBLE);
        } else {
            binding.checkedLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onItemClick(FoodImpl food, int position) {
        PresentOrAddPrice item = (PresentOrAddPrice) food;

        for (int i = 0; i < mList.size(); i++) {
            if (i == position) {
                item.setChecked(!item.isChecked());
                notifyItemChanged(i);

            } else {
                PresentOrAddPrice p = mList.get(i);
                if (p.isChecked()) {
                    p.setChecked(false);
                    notifyItemChanged(i);
                }
            }
        }
    }

    public PresentOrAddPrice getCheckedItem() {
        for (PresentOrAddPrice p : mList) {
            if (p.isChecked()) {
                return p;
            }
        }
        return null;
    }
}
